<?php
class UploadsController extends StoreAppController {

	var $name = 'StoreUploads';
	var $uses = array('StoreUpload','StoreProductUpload');
	var $permissions = array(
		'add' => array('admin','manag'),
		'index' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag'),
		'view' => array('admin','manag')
	);
	public function beforeFilter(){
		parent::beforeFilter();
//		$this->layout = 'cms';
	}
	
	function index($product_id=null) {
//		$this->StoreUpload->recursive = 0;
//		$this->set('storeUploads', $this->paginate('StoreProduct'));

		$this->_products_lists($product_id);	
		$this->set('product_id', $product_id);	
	}
	
	private function _products_lists($product_id){
		$joins = array();
		$conditions = array();
		$order = array();
		if ($product_id != null) {
			array_push($joins, array('table' => 'store_product_uploads', 'alias' => 'StoreProductUpload', 'type' => 'INNER', 'conditions' => array( 'StoreProductUpload.upload_id = StoreUpload.id')));
			$conditions['StoreProductUpload.product_id'] = $product_id;			
		}		
		$this->paginate = array('conditions' => $conditions,'limit' => 20,'joins'=>$joins);
		$this->StoreUpload->recursive = 0;
		$this->set('storeUploads', $this->paginate());	
	}

	function view($id = null,$product_id=null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid store upload', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('storeUpload', $this->StoreUpload->read(null, $id));
		$this->set('product_id', $product_id);
	}

	function add($product_id = null) {
		if (!empty($this->data)) {
			if($this->FileUpload->success){
				$this->data['StoreProductUpload']['upload_id'] = $this->StoreUpload->getLastInsertID();
				$this->StoreProductUpload->create();
				if ($this->StoreProductUpload->save($this->data)) {
					$this->Session->setFlash(__d('store','The store upload has been saved', true));
					$this->redirect(array('actions'=>'index',$this->data['StoreProductUpload']['product_id']));
				} else {
					$this->Session->setFlash(__d('store','The store upload could not be saved. Please, try again.', true));
				}
			}
		}else{
			$this->data['StoreProductUpload']['product_id'] = $product_id;
			$this->set('product_id', $product_id);
			$this->_products_lists($product_id);		
		}
	}

	function edit($id = null, $product_id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__d('store','Invalid store upload', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->StoreUpload->save($this->data)) {
				$this->Session->setFlash(__d('store','The store upload has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__d('store','The store upload could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->StoreUpload->read(null, $id);
		}
		$this->_products_lists($product_id);
		$this->set('product_id', $product_id);
	}

	function delete($id = null,$product_id=null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid id for store upload', true));
			$this->redirect(array('action'=>'index',$product_id));
		}
		if ($this->StoreUpload->delete($id)) {
			$this->Session->setFlash(__d('store','Store upload deleted', true));
			$this->redirect(array('action'=>'index',$product_id));
		}
		$this->Session->setFlash(__d('store','Store upload was not deleted', true));
		$this->redirect(array('action'=>'index',$product_id));
	}
}
?>